<?php

namespace addons\podunk\model;

use think\Model;
use app\common\library\Auth;
use think\Db;
use think\Log;
use fast\Random;
use addons\podunk\exception\Exception;

/**
 * 任务模型
 */
class Task extends Model
{
    protected $name = 'podunk_task';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [

    ];

    public static function getTaskList($app, $search)
    {
        if($search){
            $where = [
                'app'=>['LIKE',$search."%"],
                'type'=>['LIKE',$search."%"],
                'status'=>['LIKE',$search."%"],
                'identity'=>['LIKE',$search."%"]
            ];
        }else{
            $where = [];
        }

        $taskList = self::where(['user_id'=>Auth::instance()->id, 'app'=>$app])->with('device')->where(function($query) use($where){
            $query->whereOr($where);
        })->order('id desc')->paginate(10);

        if($taskList){
            return $taskList;
        }
        return null;
    }

    public function getLogAttr($value)
    {
        if(!empty($value)){
            $str = file_get_contents(substr($value,1));
            //$str = str_replace("\n","<br />",$str);
            return $str;
        }
    }

    public function device()
    {
        return $this->belongsTo('Device', 'device_id', 'id');
    }
}
